package com.sinosoft.perfect.system._.service;

import java.sql.SQLException;

import com.sinosoft.perfect.system._.dao.ISortNumberDao;
import com.sinosoft.perfect.system._.vo.SortNumber;

public class SortNumberServiceImpl implements ISortNumberService {

	private ISortNumberDao isortNumberDao;

	public void setIsortNumberDao(ISortNumberDao isortNumberDao) {
		this.isortNumberDao = isortNumberDao;
	}

	@Override
	public void sortNumber(SortNumber sortNumber) throws SQLException {
		SortNumber sortNumberHistory = isortNumberDao.selectSortHisoryInfo(sortNumber);
		String sortIdSuperId = sortNumberHistory.getSortIdSuperId();
		long oldSortNumber = sortNumberHistory.getOldSortNumber();
		sortNumber.setSortIdSuperId(sortIdSuperId);
		sortNumber.setOldSortNumber(oldSortNumber);
		long newSortNumber = sortNumber.getNewSortNumber();

		if (newSortNumber > oldSortNumber) {
			isortNumberDao.updateSortTransaction(sortNumber, false);
		}
		if (newSortNumber < oldSortNumber) {
			isortNumberDao.updateSortTransaction(sortNumber, true);
		}
	}

	@Override
	public void sortNumberRelation(SortNumber sortNumber) throws SQLException {
		SortNumber sortNumberHistory = isortNumberDao.selectSortHisoryInfoRelation(sortNumber);
		String sortIdSuperId = sortNumberHistory.getSortIdSuperId();
		long oldSortNumber = sortNumberHistory.getOldSortNumber();
		sortNumber.setSortIdSuperId(sortIdSuperId);
		sortNumber.setOldSortNumber(oldSortNumber);
		long newSortNumber = sortNumber.getNewSortNumber();

		if (newSortNumber > oldSortNumber) {
			isortNumberDao.updateSortTransactionRelation(sortNumber, false);
		}
		if (newSortNumber < oldSortNumber) {
			isortNumberDao.updateSortTransactionRelation(sortNumber, true);
		}

	}

	@Override
	public void sortNumberNoSuper(SortNumber sortNumber) throws SQLException {
		SortNumber sortNumberHistory = isortNumberDao.selectSortHisoryInfoNoSuper(sortNumber);
		long oldSortNumber = sortNumberHistory.getOldSortNumber();
		sortNumber.setOldSortNumber(oldSortNumber);
		long newSortNumber = sortNumber.getNewSortNumber();

		if (newSortNumber > oldSortNumber) {
			isortNumberDao.updateSortTransactionNoSuper(sortNumber, false);
		}
		if (newSortNumber < oldSortNumber) {
			isortNumberDao.updateSortTransactionNoSuper(sortNumber, true);
		}
	}

	/*
	 * public static void main(String args[]){ SortNumber sortNumber = new SortNumber(2,"5"
	 * ,"T_RESOURCE_SYSTEM","RESOURCE_ID","SUPER_RESOURCE_ID" ,"RESOURCE_SORT_NUMBER" ); ApplicationContext context =
	 * new ClassPathXmlApplicationContext(new String []{ "applicationContext.xml"
	 * ,"com/sinosoft/perfect/system/_/AC-system-.xml" } ); ISortNumberService i =
	 * (ISortNumberService)context.getBean("system-SortNumberServiceImpl"); try { i.sortNumber(sortNumber); } catch
	 * (SQLException e) { e.printStackTrace(); } }
	 */
}
